import sys
sys.path.append("./segmentation")
sys.path.append("./models")
from Chase import Chase
from Formatter import Formatter
from Model import Actor,Evidence,TrackSegment

if __name__ == "__main__":
    """create actor and evidence objects associated with the tracks we've built"""
    act1 = Actor()
    act1.name = "person1"
    act1.bound = True
    act1.atype = Actor.PERSON

    act2 = Actor()
    act2.name = "person2"
    act2.bound = True
    act2.atype = Actor.PERSON

    """instantiate null actor for time evidence"""
    act3 = Actor(True)

    """read in an old track and generate MovingFast segments from it (both actors use the same location vector but with different times"""
    myFormatter = Formatter()
    tfile = open("tfile.txt","r")
    lines = tfile.readlines()
    evList = myFormatter.processTrack(lines,act1)
    myChase = Chase()

    a1LocList = []
    a1StartTime = evList[0].starttime
    a1EndTime = evList[-1].endtime
    for ev in evList:
        print "adding ev: ",ev
        myChase.model.addEv(ev)
        a1LocList += ev.locations

    act1.locations = a1LocList

    ###
    # pass the evidence we've created into our chase model and calculate the conditional likelihood of the evidence we've passed it for each binding it generates
    ###
    print "\n\n\n\n"
    
    ev1 = TrackSegment(actor = act1,starttime = 50,endtime = 240,segtype = Evidence.MOVE_FAST)
    # ev1 = TrackSegment(actor = act1,starttime = 106,endtime = 248,segtype = Evidence.MOVE_FAST)
    # ev1 = TrackSegment(actor = act1,starttime = 85,endtime = 248,segtype = Evidence.MOVE_FAST)
    ev1.locations = a1LocList[(ev1.starttime-a1StartTime):(ev1.endtime-a1StartTime+1)]
    
    myChase.model.addEv(ev1)
    myChase.model.evalEv()
    print "\n\n\n added all evidence"
    # testchase.model.addEv(ev2)

    # testEv1 = TrackSegment(actor=act1,segtype=Evidence.MOVE_FAST,starttime=5,endtime=100)
    # testEv1.locations = a1LocList[(testEv1.starttime-a1StartTime):(testEv1.endtime-a1StartTime+1)]
    testEv2 = TrackSegment(actor=act1,segtype=Evidence.MOVE_FAST,starttime=96,endtime=253)
    testEv2.locations = a1LocList[(testEv2.starttime-a1StartTime):(testEv2.endtime-a1StartTime+1)]

    run1 = None
    for node in myChase.model.nodes:
        if node.name == "run1":
            run1 = node
            break
        
    print "\n\n\n"
    print ev1.locations
    print ev1.calcSpeed()
    print run1.lfunction([ev1])
        
    # print "\n\n\n"
    # print testEv1.locations
    # print testEv1.calcSpeed()
    # print run1.lfunction([testEv1])
    
    print "\n\n\n"
    print testEv2.locations
    print testEv2.calcSpeed()
    print run1.lfunction([testEv2])
    
    # #print "have "+str(len(testchase.model.hypotheses))+" hypotheses"
    # testchase.model.printXML(outf = "chase-first",mfiles = True)
    # testchase.model.printXML(outf = "chase-first-full.xml")
    # # testchase.printXML(outf = "test1.xml")
    # # print "\n\n\n\n"
    # testchase.model.addEv(ev3)
    # #print "have "+str(len(testchase.model.hypotheses))+" hypotheses"
    # testchase.model.evalEv()
    # # #testchase.printXML(outf = "test2.xml")
    # # #print "\n\n\n\n"
    # # testchase.addEv(ev2)
    # # testchase.evalEv()
    # # #print "\n\n\n\n\n"
    # # print "likelihoods: ["+",".join([str((np.e**x.likelihood) if (not math.isnan(x.likelihood)) else x.likelihood) for x in testchase.model.hypotheses])+"]"
    # testchase.model.printXML(outf = "chase-second",mfiles = True)
    # testchase.model.printXML(outf = "chase-second-full.xml")
    # # for hyp in testchase.model.hypotheses:
    # #     testchase.model.printHypXML(hyp)
    # #     print "Querying time with bindings"
    # #     print hyp.queryTime("lagtime")
    # #     print "Querying time with current time of 500"
    # #     print hyp.queryTime("lagtime",500)
    # #     print "\n\n"
